<?php

namespace app\common\logic;

use mikkle\tp_wechat\Wechat;
use think\Db;
use think\Exception;

/**
 * 微信业务逻辑
 * Class SendLogic
 */
class WechatLogic extends Logic
{
    /**
     * openid 保存
     * @param $uid
     */
    static public function wechat_save($uid)
    {
        if (self::is_wechat()) {
            $open_id = session('wechat_openid');

            if (!empty($open_id) && $uid > 0) {

                //如果存在 则返回
                $wechat = Db::name('user')->where([
                    'id' => $uid,
                    'open_id' => $open_id,
                ])->field('id,open_id')->find();
                if (!empty($wechat)) {
                    Db::name('user')->where(['id' => $uid])->update(['is_close' => 0]);
                    return;
                }

                $wechat_openid = Db::name('user')->where(['open_id' => $open_id])->field('id')->select();
                if (!empty($wechat_openid)) {
                    Db::name('user')->where(['id' => $uid])->update(['open_id' => '']);
                }

                Db::name('user')->where(['id' => $uid])->update(['open_id' => $open_id, 'is_close' => 0]);
            }
        }
    }

    static protected $topcolor = '#FF0000';
    static protected $color = '#173177';
    static protected $remark = '请及时查看！';

    /**
     * 维修完成提醒
     * @param array $param
     * @return array
     */
    public static function send_repairs_process($param = [])
    {
        try {
            $open_id = self::get_uid($param['uid']);

            if (empty($open_id)) return ['code' => 0, 'msg' => '未获取到open_id'];

            //描述信息
            $msg = [
                'first' => [
                    'value' => '你好，维修人员已完成任务。',
                ],//时间
                'keyword1' => [
                    'value' => date('Y-m-d H:i', $param['add_time']),
                    'color' => self::$color,
                ],//维修人员
                'keyword2' => [
                    'value' => $param['apply_name'],
                    'color' => self::$color,
                ],
                'remark' => [
                    'value' => '请对维修人员的服务做一下评价，谢谢！',
                ],
            ];
            $data = [
                'touser' => $open_id,
                'template_id' => config('wechat.template_repairs_finish'),
                'url' => config('xckj.domain') . url('index/order/detail', ['id' => $param['id']]),
                'data' => $msg,
            ];
            self::send_msg($data);
        } catch (Exception $e) {
            log_print($e->getMessage());
        }
    }

    /**
     * 获取用户的open_id
     * @param $uid
     * @return bool|mixed
     */
    protected static function get_uid($uid)
    {
        $open_id = Db::name('user')->where(['id' => $uid])->value('open_id');
        if (empty($open_id)) {
            log_print(['未绑定微信', Db::name('member_wechat')->getLastSql()]);
            return false;
        }

        return $open_id;
    }

    /**
     * 发送模板消息
     * @param $template
     */
    protected static function send_msg($template)
    {
        $message = [
            'touser' => $template['touser'],
            'template_id' => $template['template_id'],
            'url' => $template['url'],
            'topcolor' => self::$topcolor,
            'data' => $template['data'],
        ];
        $result = Wechat::message()->sendTemplateMessage($message);
        log_print([$message, $result]);
    }

    static function refresh_token()
    {

    }


}